import tempfile
import os
from pathlib import Path

from soj.builtin.execute.windows.winexe import WinexeExecutor
from soj.builtin.environ.workspace import WorkSpace

TEST=Path(__file__).parent.resolve()
TEST_INPUT=TEST/"input.txt"
TEST_MLE_PROG=TEST/"memory10mb.exe"
TEST_TLE_PROG=TEST/"sleep3000.exe"
TEST_RE_PROG=TEST/"re.exe"
TEST_AC_PROG=TEST/"ac.exe"
TEST_RE=True
TEST_MLE=True
TEST_TLE=True
from soj.tests.common import Test

if __name__=="__main__":
    t=Test()
    #Test MLE
    if TEST_MLE:
        with WorkSpace() as w:
            w.import_file(w.FileType.INPUT,TEST_INPUT)
            exe=WinexeExecutor(TEST_MLE_PROG,2000,1047552,w.input_file,w.output_file,w.error_file,w.report_file)
            res=exe.execute()
            print(f"Quit code {res['executor_exit_code']}")
            with w.open_handle(w.FileType.OUTPUT,mode="r") as fp:
                ans=str(fp.read()).strip()
                t.ensure(ans!="3",name="MLE_Answer")
                print(f"Answer is: {ans}")
            with w.open_handle(w.FileType.REPORT,mode="r") as fp:
                res=str(fp.read()).strip()
                print(f"report file is:\n {res}")

    #Test TLE
    if TEST_TLE:
        with WorkSpace() as w:
            w.import_file(w.FileType.INPUT,TEST_INPUT)
            exe=WinexeExecutor(TEST_TLE_PROG,2000,5000,w.input_file,w.output_file,w.error_file,w.report_file)
            res=exe.execute()
            print(f"Quit code {res['executor_exit_code']}")
            with w.open_handle(w.FileType.OUTPUT,mode="r") as fp:
                ans=str(fp.read()).strip()
                t.ensure(ans!="3",name="TLE_Answer")
                print(f"Answer is: {ans}")
            with w.open_handle(w.FileType.REPORT,mode="r") as fp:
                res=str(fp.read()).strip()
                print(f"report file is:\n {res}")
    
    #Test AC
    with WorkSpace() as w:
        w.import_file(w.FileType.INPUT,TEST_INPUT)
        exe=WinexeExecutor(TEST_AC_PROG,2000,5000,w.input_file,w.output_file,w.error_file,w.report_file)
        res=exe.execute()
        print(f"Quit code {res['executor_exit_code']}")
        with w.open_handle(w.FileType.OUTPUT,mode="r") as fp:
            ans=str(fp.read()).strip()
            t.ensure(ans=="3",name="AC_Answer")
            print(f"Answer is: {ans}")
        with w.open_handle(w.FileType.REPORT,mode="r") as fp:
            res=str(fp.read()).strip()
            print(f"report file is:\n {res}")

    #Test RE
    if TEST_RE:
        with WorkSpace() as w:
            w.import_file(w.FileType.INPUT,TEST_INPUT)
            exe=WinexeExecutor(TEST_RE_PROG,2000,5000,w.input_file,w.output_file,w.error_file,w.report_file)
            res=exe.execute()
            print(f"Quit code {res['executor_exit_code']}")
            with w.open_handle(w.FileType.OUTPUT,mode="r") as fp:
                ans=str(fp.read()).strip()
                t.ensure(ans!="3",name="RE_Answer")
                print(f"Answer is: {ans}")
            with w.open_handle(w.FileType.REPORT,mode="r") as fp:
                res=str(fp.read()).strip()
                print(f"report file is:\n {res}")
    
    
    t.print_report()
        